package com.kd.juhaowan.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.flyjingfish.shapeimageviewlib.ShapeImageView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.jauker.widget.BadgeView;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommonApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.MineResult;
import com.kd.juhaowan.ui.KefuActivity;
import com.kd.juhaowan.ui.LoginAccountActivity;
import com.kd.juhaowan.ui.MainActivity;
import com.kd.juhaowan.ui.MineDjqActivity;
import com.kd.juhaowan.ui.MineGameActivity;
import com.kd.juhaowan.ui.MineGiftActivity;
import com.kd.juhaowan.ui.MineMessageActivity;
import com.kd.juhaowan.ui.PlatFormCoinActivity;
import com.kd.juhaowan.ui.SettingActivity;
import com.kd.juhaowan.utils.GlideHelper;
import com.kd.juhaowan.utils.LogUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseFragment{

    private MineResult mineInfo;

    @BindView(R.id.item_image)
    ShapeImageView avatarView;
    @BindView(R.id.mine_name)
    TextView nameView;
    @BindView(R.id.mine_id)
    TextView idView;
    @BindView(R.id.mine_lastGame)
    TextView lastGameView;
    @BindView(R.id.mine_gameNumber)
    TextView gameNumberView;
    @BindView(R.id.mine_score)
    TextView scoreView;
    @BindView(R.id.mine_message)
    TextView messageView;
    @BindView(R.id.mine_badgeView)
    BadgeView badgeView;

    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.fragment_mine;
    }

//    private boolean IS_LOADED = false;
//    private boolean isFirst = true;
//    private Handler handler = new Handler(){
//        public void handleMessage(android.os.Message msg) {
//            Log.e("tag", "IS_LOADED="+IS_LOADED);
//            if(!IS_LOADED){
//                IS_LOADED = true;
//                //这里执行加载数据的操作
//                initData();
//            }
//            return;
//        };
//    };

private void initData(){
    EasyHttp.post(this).api(new CommonApi("api/userinfo")).request(new HttpCallbackProxy<HttpData<MineResult>>(this){
        @Override
        public void onHttpSuccess(HttpData<MineResult> result, boolean cache) {
            super.onHttpSuccess(result, cache);

            mineInfo = result.getData();
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//                bannerModels = result.getData();
            setView();
//                onRefresh(getSmartRefreshLayout());
        }
    });
}

    @Override
    protected void reInitDate() {
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        LogUtil.debugLog("MineFragment onHiddenChanged"+hidden);

    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.debugLog("MineFragment onResume");
        initData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtil.debugLog(((getActivity()!=null)?((MainActivity)getActivity()).getCurrentItem():false)+"MineFragment onHiddenChanged"+isVisibleToUser);
        if(isVisibleToUser&&getActivity()!=null&&((MainActivity)getActivity()).getCurrentItem()==2){

//            initData();
        }
    }

    @Override
    protected void onButterKnifeCreateViewAfter() {
        super.onButterKnifeCreateViewAfter();

    }
    @OnClick(R.id.mine_pingtai)
    public void gotoPingtai(View view){
        startActivity(new Intent(getActivity(), PlatFormCoinActivity.class));
    }
    @OnClick(R.id.mine_name)
    public void gotoLogin(View view){
        if(mineInfo==null){
            baseFragmentRegister.launch(new Intent(getActivity(), LoginAccountActivity.class));
        }
//        startActivity(new Intent(getActivity(), PlatFormCoinActivity.class));
    }
    @OnClick(R.id.mine_gift)
    public void gotoGift(View view){
        startActivity(new Intent(getActivity(), MineGiftActivity.class));
    }
    @OnClick(R.id.mine_djq)
    public void gotoDjq(View view){
        startActivity(new Intent(getActivity(), MineDjqActivity.class));
    }
    @OnClick(R.id.mine_game)
    public void gotoGame(View view){
        startActivity(new Intent(getActivity(), MineGameActivity.class));
    }
    @OnClick(R.id.mine_message_layout)
    public void gotomessage(View view){
        startActivity(new Intent(getActivity(), MineMessageActivity.class));
    }
    @OnClick(R.id.mine_setting)
    public void gotoSetting(View view){
        startActivity(new Intent(getActivity(), SettingActivity.class));
    }
    @OnClick(R.id.mine_kefu)
    public void gotoKefu(View view){
        startActivity(new Intent(getActivity(), KefuActivity.class));
    }
    private void setView(){
        if(!TextUtils.isEmpty(mineInfo.getAvatar())){
            GlideHelper.loadGoodsCover(Glide.with(avatarView),avatarView,mineInfo.getAvatar());
        }
        nameView.setText(mineInfo.getNickname());
        idView.setText("ID:"+mineInfo.getId());
        lastGameView.setText(mineInfo.getRecently_playing());
        gameNumberView.setText(mineInfo.getGames_played_count());
        scoreView.setText(mineInfo.getScore());
        if(mineInfo.getMsg_count()!=0){

            badgeView.setBadgeCount(mineInfo.getMsg_count());
        }
    }
}
